<?php
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
$return['gettype']=array('1'=>'充值兑换','2'=>'活动赠送','3'=>'朋友赠送');
if($_RQ['op']=='list'){
	
	$pindex = max(1, intval($_SESSION['page']));
	$psize = 15;
	$params=array('uniacid'=>$_SESSION['uniacid']);
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	
    $orderby='createtime desc';
	$total='';
	$return['list']=pdo_getslice('jy_weishop_goodscard',$params,array($pindex,$psize),$total,$fields,'',$orderby);
	$return['status']=array('0'=>'不显示','1'=>'显示');
	foreach($return['list'] as &$v){
		$v['createtime']=date('Y/m/d H:m:s',$v['createtime']);
		$getallnum = pdo_count('jy_weishop_goodscard_getlog',array('uniacid'=>$_SESSION['uniacid'],'cardid'=>$v['id']));
		if($v['t']<1){
			$v['last']='不限制';
		}else{
			$v['last']=$v['t']-$getallnum;
		}
	}
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'购物卡列表'),
			array('op'=>'edit','title'=>'添加购物卡'),
		),
		'list'=>array('data'=>'list','edit_op'=>'edit','list'=>array(
			array('name'=>'title','title'=>'购物卡名称','type'=>'string'),
			array('name'=>'gettype','title'=>'获取方式','type'=>'radio','options'=>'gettype'),								
			array('name'=>'last','title'=>'剩余数量','type'=>'string'),
			array('name'=>'status','title'=>'购卡中心显示','type'=>'radio','options'=>'status'),
			array('name'=>'createtime','title'=>'创建时间','type'=>'string'),
		),
		),
		'search'=>array(
			array('name'=>'title','width'=>15,'title'=>'名称','type'=>'string'),
		)
	);
	$return['htmls']=$htmls;
	exi($return);
}

if($_RQ['op']=='edit'){
	$id=intval($_RQ['id']);
	if($id){
	$item=pdo_get('jy_weishop_goodscard',array('id'=>$id));
	}else{
		$item=array();
	}
	$item['sendtitle']=$item['sendtitle']?$item['sendtitle']:'[nickname]:恭喜您，获得一个购物卡';
	$item['sendimage']=$item['sendimage']?$item['sendimage']:'../core/resource/images/h5game/card.png';
	$item['senddesc']=$item['senddesc']?$item['senddesc']:'购物卡的使用有时间限制，请及时使用！';
	$item['limittimes']=json_decode($item['limittimes'],true);
	$item['sendtime']=json_decode($item['sendtime'],true);
	$goods=pdo_getall('jy_weishop_goods',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'','id desc');
	foreach($goods as &$v){
		$id=$v['id'];
		$return['goods'][$id]=$v['title'];
	}
	$groups=pdo_getall('jy_weishop_goods_group',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'','id desc');
	foreach($groups as &$v){
		$id=$v['id'];
		$return['goodgroup'][$id]=$v['title'];
	}
	$setgroupids=array();
	$limitgoodcateids=str_array($item['limitgoodcateids']);
	foreach($limitgoodcateids as $cid){
		$pcate=pdo_getcolumn('jy_weishop_goods_group',array('id'=>$cid,'uniacid'=>$_SESSION['uniacid']),'parentid');
		if(!$pcate){
			$pcate=$cid;
			$cid=0;
		}
		$setgroupids[]=array('pcate'=>$pcate,'ccate'=>$cid);
	}
	$item['setgroupids']=$setgroupids;
	$setgoodsids=array();
	$limitgoodids=str_array($item['limitgoodids']);
	foreach($limitgoodids as $gid){
		$goods=pdo_get('jy_weishop_goods',array('id'=>$gid,'uniacid'=>$_SESSION['uniacid']),array('pcate','ccate'));
		$setgoodsids[]=array('goodsid'=>$gid,'pcate'=>$goods['pcate'],'ccate'=>$goods['ccate']);
	}
	$item['setgoodsids']=$setgoodsids;
	$return['item']=$item;
	$return['isdescsys']=array('0'=>'不使用','1'=>'使用');
	$return['status']=array('0'=>'不显示','1'=>'显示');
	$return['is']=array('0'=>'否','1'=>'是');
	$return['timelimittype']=array('0'=>'无限制','1'=>'获得多少天内','2'=>'时间段内');
	$catelevel=2;
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'tabs'=>array(
			array('op'=>'list','title'=>'购物卡列表'),
			array('op'=>'edit','title'=>'添加购物卡'),
		),
		'desc'=>'购物卡设置',
		'edit_tab'=>true,
		'edits'=>array(
			array(
				'title'=>'基本设置',
				'list'=>array(
					array('type'=>'string','name'=>'paixu','title'=>'排序','help'=>'越大越靠前'),
					array('type'=>'string','name'=>'title','title'=>'购物卡名称'),
					array('type'=>'image','name'=>'image','title'=>'图片'),
					array('type'=>'string','name'=>'money','title'=>'购物卡面额'),
					array('type'=>'string','name'=>'price','title'=>'购物卡价格','help'=>'花多少钱可购买本卡'),
					array('type'=>'radio','name'=>'timelimittype','title'=>'使用时间限制','options'=>'timelimittype'),
					array('type'=>'string','name'=>'timedays','title'=>'在领取后天数内有效','show'=>array('timelimittype'=>1)),
					array('type'=>'time_range','name'=>'limittimes','title'=>'在时间段内有效','show'=>array('timelimittype'=>2)),					
					array('type'=>'string','name'=>'t','title'=>'发放总数','help'=>'0为不限制'),
				)
			),
			array(
				'title'=>'领取设置',
				'list'=>array(
					array('type'=>'radio','name'=>'gettype','options'=>'gettype','title'=>'领取方式'),
					array('type'=>'radio','name'=>'status','options'=>'status','title'=>'加入购卡中心'),
					array('type'=>'switch','name'=>'issendtime','title'=>'限制发放时间'),
					array('type'=>'time_range','name'=>'sendtime','title'=>'发放时间','show'=>array('issendtime'=>1)),		
					array('type'=>'string','name'=>'getmax','title'=>'每人限领次数'),
					array('type'=>'switch','name'=>'islimitlevel','title'=>'是否限制会员等级'),
					array('type'=>'checkbox','name'=>'memberlevels','options'=>'memberlevels','title'=>'选择可领取会员等级','show'=>array('islimitlevel'=>1)),
				)
			),
			array(
				'title'=>'使用限制',
				'list'=>array(
					array('type'=>'radio','name'=>'limitusetype','options'=>'limitusetype','title'=>'优惠使用限制'),
					array('type'=>'radio','name'=>'limitgoodcatetype','options'=>'limitgoodcatetype','title'=>'商品分类使用限制'),	
					array('type'=>'array','show'=>array('limitgoodcatetype'=>1),'name'=>'setgroupids','title'=>'选择商品分类','add_tip'=>'添加一个分类',
						'list'=>array(
							array('type'=>'select','name'=>'pcate','options'=>'allgroups','title'=>'一级分类'),
							array('type'=>'select','name'=>'ccate','options'=>'allgroups','title'=>'二级分类'),
						),
						'details'=>array(
							array('type'=>'select'.$catelevel.'_line','name'=>'pcate','options'=>'pcates','title'=>'选择分类',
							'name2'=>'ccate','options2'=>'ccates','title2'=>'二级分类',
							'name3'=>'scate','options3'=>'scates','title3'=>'三级分类',
							),
						)
					),
					array('type'=>'radio','name'=>'limitgoodtype','options'=>'limitgoodtype','title'=>'商品使用限制'),
					array('type'=>'array','show'=>array('limitgoodtype'=>1),'name'=>'setgoodsids','title'=>'选择商品','add_tip'=>'添加一个商品',
						'list'=>array(
							array('type'=>'select','name'=>'pcate','options'=>'allgroups','title'=>'一级分类'),
							array('type'=>'select','name'=>'ccate','options'=>'allgroups','title'=>'二级分类'),
							array('type'=>'select','name'=>'goodsid','options'=>'goods','title'=>'商品名称'),
						),
						'details'=>array(
							array('type'=>'select3_line','name'=>'pcate','options'=>'pcategoods','title'=>'选择分类',
							'name2'=>'ccate','options2'=>'ccates','title2'=>'二级分类',
							'name3'=>'goodsid','options3'=>'goods','title3'=>'请选择商品',
							),
						)
					),
								
				)
			),
			array(
				'title'=>'推送',
				'list'=>array(
					array('type'=>'string','name'=>'sendtitle','title'=>'推送标题','help'=>'变量 [nickname] 会员昵称 [total] 购物卡张数'),
					array('type'=>'image','name'=>'sendimage','title'=>'推送封面'),
					array('type'=>'string','name'=>'senddesc','title'=>'推送说明','help'=>'变量 [nickname] 会员昵称 [total] 购物卡张数'),	
				)
			),
			array(
				'title'=>'使用说明',
				'list'=>array(
					array('type'=>'radio','name'=>'isdescsys','options'=>'isdescsys','title'=>'是否使用统一说明','help'=>'统一说明在【基础设置】中设置，如果使用统一说明，则在购物卡说明前面显示统一说明'),
					array('type'=>'string','name'=>'desc','title'=>'使用说明','help'=>'','show'=>array('isdescsys'=>0)),					
				)
			),
	)
	);
	//获取商品
	$goodsspram=array('uniacid'=>$_SESSION['uniacid'],'merchid OR'=>'showgroups=\'0\' or pcate>\'0\'');
	$onepluginname=model('jy_weishop/goods')->is_onegoods($_SESSION['current_plugin']);
	if($onepluginname){//开启独立商品
		$goodsspram['onepluginname']=$onepluginname;
	}else{
		$goodsspram['onepluginname']='';
	}
	$return['goods']=pdo_getall('jy_weishop_goods',$goodsspram,array('id','title'),'id');
	//获取分类
	$groupspram=array('uniacid'=>$_SESSION['uniacid'],'mid'=>0,'parentid'=>0);
	$onepluginname=model('jy_weishop/goods')->is_onegoods($_SESSION['current_plugin']);
	if($onepluginname){//开启独立商品
		$groupspram['onepluginname']=$onepluginname;
		$return['params']['onepluginname']=$onepluginname;
	}else{
		$groupspram['onepluginname']='';
	}
	$return['pcates']=$return['pcategoods']=pdo_getall('jy_weishop_goods_group',$groupspram,array('title','id'),'id');
	foreach($return['pcates'] as &$pv){
			$pv['ccates']=pdo_getall('jy_weishop_goods_group',array('uniacid'=>$_SESSION['uniacid'],'parentid'=>$pv['id']),array('title','id'),'id');
			foreach($pv['ccates'] as &$cv){
				$cv['scates']=pdo_getall('jy_weishop_goods_group',array('uniacid'=>$_SESSION['uniacid'],'parentid'=>$cv['id']),array('title','id'),'id');
			}
	}
	foreach($return['pcategoods'] as &$gv){
			$gv['ccates']=pdo_getall('jy_weishop_goods_group',array('uniacid'=>$_SESSION['uniacid'],'parentid'=>$gv['id']),array('title','id'),'id');
			$gv['ccates'][0]=array('id'=>0,'title'=>'默认分类');
			$gv['ccates'][0]['goods']=pdo_getall('jy_weishop_goods',array('uniacid'=>$_SESSION['uniacid'],'ccate'=>0,'pcate'=>$gv['id']),array('title','id'),'id');
			foreach($gv['ccates'] as &$gcv){
				$gcv['goods']=pdo_getall('jy_weishop_goods',array('uniacid'=>$_SESSION['uniacid'],'ccate'=>$gcv['id'],'pcate'=>$gv['id']),array('title','id'),'id');
			}
	}
	$groupspram=array('uniacid'=>$_SESSION['uniacid'],'mid'=>0,'merchid'=>0);
	$return['allgroups']=pdo_getall('jy_weishop_goods_group',$groupspram,array('title','id'),'id');
	$return['allgroups'][0]=array('id'=>0,'title'=>'默认等级');
	$memberlevels=pdo_getall('jy_weishop_member_level',array('uniacid'=>$_SESSION['uniacid'],'merchid'=>0),array('id','title'),'','id desc');
	foreach($memberlevels as $v){
		$return['memberlevels'][$v['id']]=$v['title'];
	}
	$return['memberlevels'][0]='默认等级';
	$return['limitusetype']=array('0'=>'无优惠使用限制','1'=>'不可与促销优惠同时使用','2'=>'不可与会员折扣同时使用','3'=>'不可与促销优惠和会员折扣同时使用');
	$return['limitgoodtype']=array('0'=>'不添加商品限制','1'=>'允许以下商品使用');
	$return['limitgoodcatetype']=array('0'=>'不添加商品分类限制','1'=>'允许以下商品分类使用');
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	$data=array_fields(array('islimitlevel','memberlevels','limitusetype','limitgoodcatetype','limitgoodtype','money','issendtime','sendtime','paixu','title','getmax','price','gettype','image','timelimittype','timedays','limittimes','t','status','sendtitle','senddesc','sendimage','desc','isdescsys'),$postdata);
	$limitgoodcateids=';';
	$setgroupids=$postdata['setgroupids'];
	foreach($setgroupids as $sv){
		$cid=$sv['ccate']?:$sv['pcate'];
		if(strpos($limitgoodcateids,';'.$cid.';') === false){
			$limitgoodcateids .=$cid.';';
		}
	}
	$data['limitgoodcateids']=$limitgoodcateids;
	$limitgoodids=';';
	$setgoodsids=$postdata['setgoodsids'];
	foreach($setgoodsids as $sv){
		$goodsid=$sv['goodsid'];
		if(strpos($limitgoodids,';'.$goodsid.';') === false){
			$limitgoodids .=$goodsid.';';
		}
	}
	$data['limitgoodids']=$limitgoodids;
	$data['limittimes']=json_encode($data['limittimes']);
	$data['sendtime']=json_encode($data['sendtime']);
	$id=intval($postdata['id']);
	if($id){
				pdo_update('jy_weishop_goodscard',$data,array('id'=>$id));
				exi('设置成功！','','list');
	}else{
			    $data['uniacid']=$_SESSION['uniacid'];
				$data['createtime']=TIMESTAMP;
				pdo_insert('jy_weishop_goodscard',$data);
				exi('添加成功！','','list');
	}
}

if($_RQ['op']=='del'){

	$id=intval($_RQ['id']);
	if($id){
		pdo_delete('jy_weishop_goodscard',array('id'=>$id));	
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				pdo_delete('jy_weishop_goodscard',array('id'=>$id));
			}
		}
	}
	exi('删除成功！');
}
?>